package de.s9mtdev.lol_destiny_home;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.listeners.IRCEventListener;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.FlowLayout;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class InSession extends JFrame implements IRCEventListener {

	private JPanel contentPane;
	private String sessionkey;
	private String sessionpass;
	private int currentUsers = 1;
	private JLabel lblNewLabel;
	private Session session;
	private JPanel panel;
	private FlashView panel_1;
	private FlashView panel_2;
	private FlashView panel_3;
	private FlashView panel_4;
	private FlashView panel_5;
	private JPanel panel_6;
	private BuffView panel_7;
	private BossView panel_8;
	private BuffView panel_9;
	private BuffView panel_10;
	private BossView panel_11;
	private BuffView panel_12;
	private JButton btnExitSession;
	private JFrame home;
	private RiftView panel_13;

		/**
		 * @wbp.parser.constructor
		 */
		public InSession(JFrame h, Session s) {
			home = h;
			sessionkey = Utils.generateRnd(5);
			sessionpass = Utils.generateRnd(3);
			session = s;
			session.addIRCEventListener(this);
			session.join("#dy_" + sessionkey);
			init();
	}
		
		public InSession(JFrame h, Session s, String k, String p) {
			home = h;
			sessionkey = k;
			sessionpass = p;
			session = s;
			session.addIRCEventListener(this);
			session.join("#dy_" + sessionkey, sessionpass);
			init();
		}
		
	private void init() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setSize(500, 800);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] {485, 0};
		gbl_contentPane.rowHeights = new int[] {40, 400, 50, 50, 90, 0};
		gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 1.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		lblNewLabel = new JLabel();
		lblNewLabel.setFont(new Font("Arial", 0, 20));
		updateText();
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.fill = GridBagConstraints.BOTH;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 0;
		contentPane.add(lblNewLabel, gbc_lblNewLabel);
		
		panel_13 = new RiftView(this);
		GridBagConstraints gbc_panel_13 = new GridBagConstraints();
		gbc_panel_13.insets = new Insets(0, 0, 5, 0);
		gbc_panel_13.fill = GridBagConstraints.BOTH;
		gbc_panel_13.gridx = 0;
		gbc_panel_13.gridy = 1;
		contentPane.add(panel_13, gbc_panel_13);
				
		panel = new JPanel();
		panel.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 2;
		contentPane.add(panel, gbc_panel);
		panel.setLayout(new GridLayout(0, 5, 0, 0));
		
		panel_1 = new FlashView(this, "Top");
		panel.add(panel_1);
		
		panel_2 = new FlashView(this, "Jungle");
		panel.add(panel_2);
		
		panel_3 = new FlashView(this, "Mid");
		panel.add(panel_3);
		
		panel_4 = new FlashView(this, "AD");
		panel.add(panel_4);
		
		panel_5 = new FlashView(this, "Supp");
		panel.add(panel_5);
		
		panel_6 = new JPanel();
		panel.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel_6 = new GridBagConstraints();
		gbc_panel_6.fill = GridBagConstraints.BOTH;
		gbc_panel_6.insets = new Insets(0, 0, 5, 0);
		gbc_panel_6.gridx = 0;
		gbc_panel_6.gridy = 3;
		contentPane.add(panel_6, gbc_panel_6);
		panel_6.setLayout(new GridLayout(2, 3, 0, 0));
		
		panel_7 = new BuffView(this, "Left Blue");
		panel_6.add(panel_7);
		
		panel_8 = new BossView(this, "Baron");
		panel_6.add(panel_8);
		
		panel_9 = new BuffView(this, "Right Red");
		panel_6.add(panel_9);
		
		panel_10 = new BuffView(this, "Left Red");
		panel_6.add(panel_10);
		
		panel_11 = new BossView(this, "Dragon");
		panel_6.add(panel_11);
		
		panel_12 = new BuffView(this, "Right Blue");
		panel_6.add(panel_12);
		
		
		
	    Timer timer = new javax.swing.Timer(900, new ActionListener() {    	   
    	    @Override
    	    public void actionPerformed(final ActionEvent e) {    	        
    	        panel_13.repaint();
    	        panel_1.repaint();
    	        panel_2.repaint();
    	        panel_3.repaint();
    	        panel_4.repaint();
    	        panel_5.repaint();
    	        panel_7.repaint();
    	        panel_8.repaint();
    	        panel_9.repaint();
    	        panel_10.repaint();
    	        panel_11.repaint();
    	        panel_12.repaint();
    	    }
    	});
	    timer.start();
		
		
		
		btnExitSession = new JButton("Exit Session");
		btnExitSession.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				session.sayRaw("part #dy_" + sessionkey);
				setVisible(false);			
				home.setVisible(true);
				dispose();
			}
		});
		GridBagConstraints gbc_btnExitSession = new GridBagConstraints();
		gbc_btnExitSession.gridx = 0;
		gbc_btnExitSession.gridy = 4;
		contentPane.add(btnExitSession, gbc_btnExitSession);
	}

	@Override
	public void receiveEvent(IRCEvent e) {
		if ( e.getType() == jerklib.events.IRCEvent.Type.JOIN ) {
			if (e.getRawEventData().contains(sessionkey)) {
				currentUsers++;
				updateText();
			}
		} else if ( e.getType() == jerklib.events.IRCEvent.Type.PART ) {
			if (e.getRawEventData().contains(sessionkey)) {
				currentUsers--;
				updateText();
			}
		} else if ( e.getType() == jerklib.events.IRCEvent.Type.QUIT ) {
				currentUsers--;
				updateText();
		} else if ( e.getType() == jerklib.events.IRCEvent.Type.JOIN_COMPLETE ) {
			if (session.getChannel("#dy_" + sessionkey) != null) {
				session.sayRaw("mode #dy_" + sessionkey + " +k " + sessionpass);
			}
		}		
	}
	
	private void updateText() {
		lblNewLabel.setText(sessionkey + ":" + sessionpass + " - Users: " + currentUsers +"/5");
	}
	
	public String getSessionkey() {
		return sessionkey;
	}

	public Session getSession() {
		return session;
	}


}
